﻿/*
 DotNetNuke - http://www.dotnetnuke.com
 Copyright (c) 2002-2010 by DotNetNuke Corp.

 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
 documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
 the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
 to permit persons to whom the Software is furnished to do so, subject to the following conditions:

 The above copyright notice and this permission notice shall be included in all copies or substantial portions 
 of the Software.

 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
 TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
 CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
 DEALINGS IN THE SOFTWARE.

*/
using System;
using System.Collections;
using System.Data;

using System.Web;

using DotNetNuke.Common;
using DotNetNuke.Common.Utilities;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Entities.Portals;
using DotNetNuke.Entities.Users;
using DotNetNuke.Modules.UserDefinedTable.Security;



namespace DotNetNuke.Modules.UserDefinedTable
{


  public class DataTypeCreatedBy : DataType, IEmailAdressSource, IUserSource
  {




    public string GetEmailAddress(string FieldName, DataRow row)
    {
      return this.GetUser(FieldName, row).Email;
    }




    public UserInfo GetUser(string FieldName, DataRow row)
    {
      string username = "";
      if (row.Table.Columns.Contains(FieldName + DataTableColumn.Appendix_Original))
      {
        username = Utilities.CStrN((row[FieldName + DataTableColumn.Appendix_Original]), "");
      }
      else
      {
        username = Utilities.CStrN((row[FieldName]), "");
      }
      if ( !string.IsNullOrEmpty(username) )
      {
        return UserController.GetUserByName(DotNetNuke.Common.Globals.GetPortalSettings().PortalId, username);
      }
      return new UserInfo();
    }





    public override void RenderValuesToHtmlInsideDataSet(DataSet ds, int ModuleId, bool noScript)
    {
      ArrayList list = new ArrayList();
      DataTable table = ds.Tables[DataSetTableName.Data];

      string str;

      foreach (DataRow current in ds.Tables[DataSetTableName.Fields].Rows)
        if ((string)current[FieldsTableColumn.Type] == this.Name)
        {
          str = (string)current[FieldsTableColumn.Title];
          list.Add(str);
          table.Columns.Add(new DataColumn(str + DataTableColumn.Appendix_Original, typeof(string)));
          table.Columns.Add(new DataColumn(str + DataTableColumn.Appendix_Caption, typeof(string)));
          table.Columns.Add(new DataColumn(str + DataTableColumn.Appendix_Url, typeof(string)));
        }

      if ((ds != null) & (list.Count > 0))
      {
        bool flag3;
        PortalSettings currentPortalSettings = null;
        if (HttpContext.Current != null)
          currentPortalSettings = PortalController.GetCurrentPortalSettings();
        
        Hashtable moduleSettings = new ModuleController().GetModuleSettings(ModuleId);
        bool expression = Utilities.CBoolN((moduleSettings[SettingName.UserLinkNewWindow]), false);
        bool flag2 = Utilities.CBoolN((moduleSettings[SettingName.CnCLink]), false);

        if (!moduleSettings.ContainsKey(SettingName.UserLinkCaption))
          flag3 = false;

        else
          flag3 = ((string)moduleSettings[SettingName.UserLinkCaption]).Equals("UserName", StringComparison.OrdinalIgnoreCase);

        string str2, str4, str6, strCreatedBy;
        int nullInteger;

        foreach (DataRow row2 in table.Rows)
        {
          foreach (string FieldName in list)
          {
            strCreatedBy = (string)row2[FieldName];
            str4 = strCreatedBy;
            nullInteger = Null.NullInteger;
            str6 = string.Empty;

            if (currentPortalSettings != null)
            {
              if (!flag3)
                str4 = ModuleSecurity.BestUserName(strCreatedBy, currentPortalSettings.PortalId);

              if (flag2)
              {
                nullInteger = ModuleSecurity.UserId(strCreatedBy, currentPortalSettings.PortalId);
                if (nullInteger > 0)
                  str6 = HttpUtility.HtmlEncode(DotNetNuke.Common.Globals.LinkClick("userid=" + nullInteger.ToString(), currentPortalSettings.ActiveTab.TabID, ModuleId));

              }
            }
            if ( flag2 && !string.IsNullOrEmpty(str6) )
              str2 = string.Format("<!--{1}--><a href=\"{0}\"{2}>{1}</a>", str6, str4, (expression ? " target=\"_blank\"" : "").ToString());

            else
              str2 = str4;

            row2[FieldName] = str2;
            row2[FieldName + DataTableColumn.Appendix_Original] = strCreatedBy;
            row2[FieldName + DataTableColumn.Appendix_Caption] = str4;

            if ( !string.IsNullOrEmpty(str6) )
              row2[FieldName + DataTableColumn.Appendix_Url] = str6;
          }
        }
      }
    }





    public override bool IsUserDefinedField
    {
      get
      {
        return false;
      }
    }




    public override string Name
    {
      get
      {
        return "CreatedBy";
      }
    }




    public override bool SupportsHideOnEdit
    {
      get
      {
        return true;
      }
    }




    public override bool SupportsSearch
    {
      get
      {
        return true;
      }
    }


  }

}

